import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
import {Router} from '@angular/router';
import * as moment from 'moment';
import {LoginService} from 'app/lib/service/login.service';
import {LocalStorageService} from '../../lib/service/local-storage.service';
import {adSystemApiUrl, AliyunOssOutputUrl} from "../../../config/api";
import {HttpService} from "../../lib/service/http.service";
import {MyShopService} from '../../member/myshop.service';

export class LoginData {
  code: number;
  data: {
    user: {
      id: number,
      user_uuid: string,
      name: string,
      nick_name: string,
      email: string,
      mobile: string,
      remember_token: string,
      sex: string,
      avatar: string,
      isLock: number,
      lock_time: Date,
      openning_time: Date,
      created_at: Date,
      updated_at: Date,
      remark: string,
      time: string,
    },
    token: {
      "access_token": string,
      "token_type": string,
      "expires_in": number,
      "expires_at": Date
    }
  };
  message: string;
  time: string;
}

@Component({
  selector: 'app-login',
  templateUrl: 'login.component.html',
  styleUrls: ['login.component.scss'],
  providers: [LoginService, LocalStorageService, HttpService , MyShopService],
})
export class LoginComponent implements OnInit {


  loginData = new LoginData();

  username: string;

  password: any;

  public alerts: any;

  public tableId: string;

  public vUrl: string;

  public myAppletList: any;

  public isLoading: boolean;

  aliyunOssOutputUrl: string = AliyunOssOutputUrl;


  constructor( private loginService: LoginService,
               private route: Router,
               private localStorageService: LocalStorageService,
              private httpService: HttpService,
               private myshopService: MyShopService,
  ) {

  }

  ngOnInit() {

  }

  login() {
    this.isLoading = true;
    this.loginService.doLogin(this.username, this.password).subscribe(
      rs => {
        console.log('login--', rs);
        if (rs.code === 200) {
          this.loginData = rs;
          const time = new Date(rs.data.auth_expire_at);
          const now_time = time.getTime() / 1000;
          // @ts-ignore
          // this.loginData.data.user.time = now_time + '';
          // 清理之前登录的信息
          this.localStorageService.clearAll();
          // 保存当前的信息
          this.localStorageService.setNowApplet(rs.data);
          this.localStorageService.setUserInfo(rs.data.applet_member);
          // this.localStorageService.setToken(rs.data.auth_sign);
          // @ts-ignore // this.loginData.data['nowApplet']
          return this.redirectToDefaultApp();
        } else {
          this.isLoading = false;
          return this.alerts = [{
            type: 'danger',
            msg: rs.message,
          }];
        }
      },
      err => console.log(err),
    );
  }

  getAppletList() {
    const id = this.localStorageService.getUserInfo().id;
    const url = `${adSystemApiUrl}/v1/member/applets/list?userID=${id}`;
    return this.httpService.httpGetAuth(url).subscribe(
      res => {
        this.myAppletList = res.data.data;
      }
    );
  }
  redirectToDefaultApp(applet) {
    // this.localStorageService.setNowApplet(applet);
    this.myshopService.getMyShop().subscribe(res => {
      // this.shop_list = res.data;
        let shop = res.data[0];
        this.localStorageService.setShop(shop);
        this.route.navigate(['/member/product/list/all']);
      // this.mshopService.setShop(res.data[0].id);
    });
    if (this.route.url !== '/home/login') {
      this.isLoading = false;
    }
  }


}
